﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using dotnetCHARTING;
using ChartExtents;
using System.Collections.Generic;
using System.Drawing;

public partial class main_dataAnalyze_HJQX : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LineDAOs lineDAOs = LineDAOs.getInstance();
            List<String> lineNameList = lineDAOs.getLineNameList();
            for (int i = 0; i < lineNameList.Count; i++)
            {
                dropLine.Items.Add(lineNameList[i]);
            }
            TowerDAOs towerDAOs = TowerDAOs.getInstance();
            List<String> towerNameList = towerDAOs.getTowerNameList(lineNameList[0]);
            for (int j = 0; j < towerNameList.Count; j++)
            {
                dropTower.Items.Add(towerNameList[j]);
            }
            this.txtCJSK.Text = towerDAOs.getNowTime(dropTower.SelectedValue, "温度");
            this.txtWD.Text = towerDAOs.getNow(dropTower.SelectedValue, "温度");
            this.txtSD.Text = towerDAOs.getNow(dropTower.SelectedValue, "湿度");
            this.txtFS.Text = towerDAOs.getNow(dropTower.SelectedValue, "风速");
            this.txtFX.Text = towerDAOs.getNow(dropTower.SelectedValue, "风向");
            this.txtQY.Text = towerDAOs.getNow(dropTower.SelectedValue, "气压");
            this.txtYL.Text = towerDAOs.getNow(dropTower.SelectedValue, "雨量");
            this.txtRZQD.Text = towerDAOs.getNow(dropTower.SelectedValue, "日照");
        }
    }

    protected void dropLine_SelectedIndexChanged(object sender, EventArgs e)
    {
        TowerDAOs towerDAOs = TowerDAOs.getInstance();
        dropTower.Items.Clear();
        List<String> towerNameList = towerDAOs.getcorrespondingTowerNameList_HJQX(dropLine.SelectedValue);
        for (int j = 0; j < towerNameList.Count; j++)
        {
            dropTower.Items.Add(towerNameList[j]);
        }
        this.txtCJSK.Text = towerDAOs.getNowTime(dropTower.SelectedValue, "温度");
        this.txtWD.Text = towerDAOs.getNow(dropTower.SelectedValue, "温度");
        this.txtSD.Text = towerDAOs.getNow(dropTower.SelectedValue, "湿度");
        this.txtFS.Text = towerDAOs.getNow(dropTower.SelectedValue, "风速");
        this.txtFX.Text = towerDAOs.getNow(dropTower.SelectedValue, "风向");
        this.txtQY.Text = towerDAOs.getNow(dropTower.SelectedValue, "气压");
        this.txtYL.Text = towerDAOs.getNow(dropTower.SelectedValue, "雨量");
        this.txtRZQD.Text = towerDAOs.getNow(dropTower.SelectedValue, "日照");
    }

    private void Drawing(int i)
    {
        Chart[] chart = { this.Chart1, this.Chart2, this.Chart3, this.Chart4, this.Chart5, this.Chart6, this.Chart7 };
        chart[i].Type = ChartType.Combo;//Horizontal;
        chart[i].Size = "600x350";
        chart[i].TempDirectory = "images";
        chart[i].Debug = true;
        chart[i].XAxis.TimeInterval = TimeInterval.Day;
        chart[i].XAxis.Scale = Scale.Time;
        SeriesCollection mySC = GetDataSource(i);
        if (i == 0)
        {
            chart[i].YAxis.Name = "温度";
            TowerDAOs towerDAOs = TowerDAOs.getInstance();
            DataSet ds = towerDAOs.getAlarm(dropTower.SelectedValue, RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
            int j = ds.Tables[0].Rows.Count;
            String[] description = new String[j];
            for (int b = 0; b < j; b++)
            {
                description[b] = ds.Tables[0].Rows[b]["act_description"].ToString();
                foreach (Element myE in mySC[0].Elements)
                {
                    if (description[b].Equals("温度黄色告警"))
                        myE.Color = Color.Yellow;
                    else if (description[b].Equals("温度橙色告警"))
                        myE.Color = Color.Orange;
                    else if (description[b].Equals("温度红色告警"))
                        myE.Color = Color.Red;
                }
                chart[i].XAxis.Markers.Add(new AxisMarker("", Color.FromArgb(50, Color.Red), ds.Tables[0].Rows[b]["start_time"], ds.Tables[0].Rows[b]["stop_time"]));
            }
        }
        else if (i == 1)
        {

            chart[i].YAxis.Name = "湿度";
        }
        else if (i == 2)
        {
            TowerDAOs towerDAOs = TowerDAOs.getInstance();
            DataSet ds = towerDAOs.getAlarm(dropTower.SelectedValue, RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
            int j = ds.Tables[0].Rows.Count;
            String[] description = new String[j];
            for (int b = 0; b < j; b++)
            {
                description[b] = ds.Tables[0].Rows[b]["act_description"].ToString();
                foreach (Element myE in mySC[0].Elements)
                {
                    if (description[b].Equals("大风黄色告警"))
                        myE.Color = Color.Yellow;
                    else if (description[b].Equals("大风橙色告警"))
                        myE.Color = Color.Orange;
                    else if (description[b].Equals("大风红色告警"))
                        myE.Color = Color.Red;
                }
                chart[i].XAxis.Markers.Add(new AxisMarker("", Color.FromArgb(50, Color.Red), ds.Tables[0].Rows[b]["start_time"], ds.Tables[0].Rows[b]["stop_time"]));
            }
            chart[i].YAxis.Name = "风速";
        }
        else if (i == 3)
        {
            chart[i].YAxis.Name = "风向";
        }
        else if (i == 4)
        {
            chart[i].YAxis.Name = "气压";
        }
        else if (i == 5)
        {
            TowerDAOs towerDAOs = TowerDAOs.getInstance();
            DataSet ds = towerDAOs.getAlarm(dropTower.SelectedValue, RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
            int j = ds.Tables[0].Rows.Count;
            String[] description = new String[j];
            for (int b = 0; b < j; b++)
            {
                description[b] = ds.Tables[0].Rows[b]["act_description"].ToString();
                foreach (Element myE in mySC[0].Elements)
                {
                    if (description[b].Equals("雨量黄色告警"))
                        myE.Color = Color.Yellow;
                    else if (description[b].Equals("雨量橙色告警"))
                        myE.Color = Color.Orange;
                    else if (description[b].Equals("雨量红色告警"))
                        myE.Color = Color.Red;
                }
                chart[i].XAxis.Markers.Add(new AxisMarker("", Color.FromArgb(50, Color.Red), ds.Tables[0].Rows[b]["start_time"], ds.Tables[0].Rows[b]["stop_time"]));
            }
            chart[i].YAxis.Name = "雨量";
        }
        else
        {
            chart[i].YAxis.Name = "日照";
        }
        
        chart[i].DefaultSeries.Type = SeriesType.Spline;
        chart[i].TitleBox.Position = TitleBoxPosition.FullWithLegend;
        chart[i].Series.StartDate = RadDatePicker1.SelectedDate.Value.Date;
        chart[i].Series.EndDate = RadDatePicker2.SelectedDate.Value.Date;
        // chart[i].DefaultSeries.Type = SeriesTypeFinancial.Close;
        //SeriesCollection mySC = GetDataSource();

        chart[i].SeriesCollection.Add(mySC);

    }
    private SeriesCollection GetDataSource(int j)
    {
        TowerDAOs towerDAOs = TowerDAOs.getInstance();
        SeriesCollection SC = new SeriesCollection();
        DataSet ds = new DataSet();
        if (j == 0)
        {
           ds = towerDAOs.getSpineB(dropTower.SelectedValue, "温度", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
        }
        else if(j==1)
        {
           ds = towerDAOs.getSpineB(dropTower.SelectedValue, "湿度", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
        }
        else if (j == 2)
        {
             ds = towerDAOs.getSpineB(dropTower.SelectedValue, "风速", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
        }
        else if (j == 3)
        {
            ds = towerDAOs.getSpineB(dropTower.SelectedValue, "风向", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
        }
        else if (j == 4)
        {
            ds = towerDAOs.getSpineB(dropTower.SelectedValue, "气压", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
        }
        else if (j == 5)
        {
            ds = towerDAOs.getSpineB(dropTower.SelectedValue, "雨量", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
        }
        else
        {
             ds = towerDAOs.getSpineB(dropTower.SelectedValue, "日照", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
        }

        int i = ds.Tables[0].Rows.Count;
        if (i != 0)
        {
            if (i > 50)
            {
                i = 50;
            }
            Series s = new Series();
            if (j == 0)
            {
                s.Name = "温度";
                for (int b = 0; b < i; b++)
                {
                    Element e = new Element();
                    e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
                    e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
                    s.Elements.Add(e);
                }
                SC.Add(s);
                this.Chart1.Visible = true;
            }
            else if (j == 1)
            {
                s.Name = "湿度";
                for (int b = 0; b < i; b++)
                {
                    Element e = new Element();
                    e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
                    e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
                    s.Elements.Add(e);
                }
                SC.Add(s);
                this.Chart2.Visible = true;
            }
            else if(j==2)
            {
                s.Name = "风速";
                for (int b = 0; b < i; b++)
                {
                    Element e = new Element();
                    e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
                    e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
                    s.Elements.Add(e);
                }
                SC.Add(s);
                this.Chart3.Visible = true;
            }
            else if (j == 3)
            {
                s.Name = "风向";
                for (int b = 0; b < i; b++)
                {
                    Element e = new Element();
                    e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
                    e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
                    s.Elements.Add(e);
                }
                SC.Add(s);
                this.Chart4.Visible = true;
            }
            else if (j == 4)
            {
                s.Name = "气压";
                for (int b = 0; b < i; b++)
                {
                    Element e = new Element();
                    e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
                    e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
                    s.Elements.Add(e);
                }
                SC.Add(s);
                this.Chart5.Visible = true;
            }
            else if (j == 5)
            {
                s.Name = "雨量";
                for (int b = 0; b < i; b++)
                {
                    Element e = new Element();
                    e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
                    e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
                    s.Elements.Add(e);
                }
                SC.Add(s);
                this.Chart6.Visible = true;
            }
            else if (j == 6)
            {
                s.Name = "日照强度";
                for (int b = 0; b < i; b++)
                {
                    Element e = new Element();
                    e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
                    e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
                    s.Elements.Add(e);
                }
                SC.Add(s);
                this.Chart7.Visible = true;
            }
        }
        return SC;
    }
    protected void dropTower_SelectedIndexChanged(object sender, EventArgs e)
    {
        TowerDAOs towerDAOs = TowerDAOs.getInstance();
        this.txtCJSK.Text = towerDAOs.getNowTime(dropTower.SelectedValue, "温度");
        this.txtWD.Text = towerDAOs.getNow(dropTower.SelectedValue, "温度");
        this.txtSD.Text = towerDAOs.getNow(dropTower.SelectedValue, "湿度");
        this.txtFS.Text = towerDAOs.getNow(dropTower.SelectedValue, "风速");
        this.txtFX.Text = towerDAOs.getNow(dropTower.SelectedValue, "风向");
        this.txtQY.Text = towerDAOs.getNow(dropTower.SelectedValue, "气压");
        this.txtYL.Text = towerDAOs.getNow(dropTower.SelectedValue, "雨量");
        this.txtRZQD.Text = towerDAOs.getNow(dropTower.SelectedValue, "日照");
    }
    protected void btnQX_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 7; i++)
        {
            Drawing(i);
        }
    }
}
